using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerObject : MonoBehaviour
{
    private static PlayerObject instance;
    public static PlayerObject Instance => instance;

    public int Hp;
    public int MaxHp;
    public float MoveSpeed;
    public float RoundSpeed;
    private Quaternion TagetQuaternion;
    public bool IsDead;

    private Vector3 FrontPos;
    private Vector3 NowPos;
    private void Awake()
    {
        instance = this;
    }

    public void Dead()
    {
        IsDead = true;
        GameOverPanel.Instance.ShowMe();    
    }

    public void Wound()
    {
        if (IsDead) return;
        Hp -= 1;
        GamePanel.Instance.ChangeHp(Hp);
        if (Hp <= 0)
        {
            Hp = 0;
            Dead();
        }
    }

    private float Hvalue;
    private float Vvalue;
    private void Update()
    {
        Hvalue = Input.GetAxisRaw("Horizontal");
        Vvalue = Input.GetAxisRaw("Vertical");
        if (Hvalue == 0)
        {
            TagetQuaternion = Quaternion.identity;
        }
        else
        {
            TagetQuaternion = Hvalue < 0 ? Quaternion.AngleAxis(20,Vector3.forward): Quaternion.AngleAxis(-20, Vector3.forward); 
        }
        this.transform.rotation = Quaternion.Slerp(this.transform.rotation, TagetQuaternion, RoundSpeed * Time.deltaTime);
        FrontPos= this.transform.position;
        this.transform.Translate(Vector3.forward * Vvalue * MoveSpeed * Time.deltaTime);
        this.transform.Translate(Vector3.right * Hvalue * MoveSpeed * Time.deltaTime, Space.World);
        NowPos = Camera.main.WorldToScreenPoint(this.transform.position);
        if(NowPos.x<=0||NowPos.x>=Screen.width)
        {
            this.transform.position = new Vector3(FrontPos.x, this.transform.position.y, this.transform.position.z);
        }
        if (NowPos.y <= 0 || NowPos.y >= Screen.height)
        {
            this.transform.position = new Vector3(this.transform.position.x, FrontPos.y, this.transform.position.z);
        }
    }
}
